package com.goldplusgold.tradedelay.app.market.widget.chartview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.Html;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;

import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.network.lib.http.protobuf.Market;
import com.goldplusgold.support.lib.utils.StringConvertNumber;
import com.goldplusgold.tradedelay.app.market.pojo.CrossBean;
import com.goldplusgold.tradedelay.app.market.pojo.StickData;
import com.goldplusgold.tradedelay.app.market.widget.chartutil.ColorUtil;
import com.goldplusgold.tradedelay.app.market.widget.chartutil.DrawUtils;
import com.goldplusgold.tradedelay.app.market.widget.chartutil.GridUtils;
import com.goldplusgold.tradedelay.app.market.widget.chartutil.IndexParseUtil;
import com.goldplusgold.tradedelay.app.market.widget.chartutil.LinePoint;
import com.goldplusgold.tradedelay.app.market.widget.chartutil.LineUtil;

import java.util.List;


/**
 * Created by Arvin on 2016/10/25.
 * K线View
 */
public class KLineView extends ChartView implements ChartConstant {
    //烛形图加空白的宽度和烛形图宽度之比
    public static final float WIDTH_SCALE = 1.1f;
    //烛形图和右侧空白的宽度
    public float DEFUALT_WIDTH = 15;
    //y轴最大值
    protected double yMax;
    //y轴最小值
    protected double yMin;
    protected float yUnit;
    protected float xUnit;
    //chart 触摸文本消失监听
    protected OnCrossListener onCrossListener;
    //K线所有数据
    private List<StickData> data;
    private Market.InstType instType;
    //K线展示的数据
    //一屏烛形图数量
    private int drawCount;
    //没两个烛形图x轴的距离
    private float candleXDistance;
    //当前画图偏移量（往右滑动之后）
    private int offset;
    private boolean isCrossLineVisiable;
    private float xSize;
    private float ySize;
    private float maValueSize;
    private StickData currentData;

    private static float lineSmoothness = 0.2f;
    static Paint line_paint = new Paint();

    int sma_page_start_index = 0;
    int sma_page_end_inde = 0;

    long volCountMax = 0L;

    static {
        line_paint.setAntiAlias(true);
        line_paint.setStrokeWidth(2.0f);
        line_paint.setStyle(Paint.Style.STROKE);
        line_paint.setStrokeJoin(Paint.Join.ROUND);
    }

    private ScaleGestureDetector mScaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.SimpleOnScaleGestureListener() {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            if (data == null) return super.onScale(detector);
            //放大是由1变大，缩小是由1变小
            float scale = detector.getScaleFactor();
            //这个变化太快，把scale变 慢一点
            scale = 1 + ((scale - 1) * 0.4f);
            drawCount = (int) (mWidth / DEFUALT_WIDTH);
            if (scale < 1 && drawCount >= data.size()) {
                //缩小时，当缩到一屏显示完时，则不再缩小
            } else if (scale > 1 && drawCount < 20) {
                //放大时，当一屏少于20个时，则不再放大
            } else {
                DEFUALT_WIDTH = DEFUALT_WIDTH * scale;
                invalidate();
            }
            return super.onScale(detector);
        }
    });

    public KLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.KLineView);
        xSize = mTypedArray.getDimension(R.styleable.KLineView_kXSize, 0);
        ySize = mTypedArray.getDimension(R.styleable.KLineView_kYSize, 0);
        maValueSize = mTypedArray.getDimension(R.styleable.KLineView_maValue, 0);
        mTypedArray.recycle();
        lineType = ChartView.TYPE_KLINE;
    }

    public void setOnCrossListener(OnCrossListener onCrossListener) {
        this.onCrossListener = onCrossListener;
    }

    @Override
    protected boolean onViewScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        if (data != null && drawCount < data.size() && Math.abs(distanceX) > DEFUALT_WIDTH) {
            int temp = offset + (int) (0 - distanceX / DEFUALT_WIDTH);
            if (temp < 0 || temp + drawCount > data.size()) {

            } else {
                offset = temp;
                postInvalidate();
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector != null)
            gestureDetector.onTouchEvent(event);
        if (mScaleGestureDetector != null)
            mScaleGestureDetector.onTouchEvent(event);
        return true;
    }

    @Override
    protected void init() {
        if (data == null) return;
        drawCount = (int) (mWidth / DEFUALT_WIDTH);
        candleXDistance = drawCount * WIDTH_SCALE;
        if (data != null && data.size() > 0) {
            getShowList(offset);
        }
        double max = 0f;
        double min = Float.MAX_VALUE;
        volCountMax  = 0;
        for (int i = sma_page_start_index; i < sma_page_end_inde; i++) {
            double low = data.get(i).getK_lineYmin();
            double high =  data.get(i).getK_lineYmax();
            if(high >= max){
                max = high;
            }
            if(low <= min){
                min = low;
            }
            volCountMax = data.get(i).getC_lineYmax() > volCountMax ? (long) data.get(i).getC_lineYmax() : volCountMax;
        }
        yMax = max;
        yMin = min;
        yUnit = (float) (yMax - yMin) / (mainH - 40);
        xUnit = mWidth / drawCount;

    }

    @Override
    protected void drawGrid(Canvas canvas) {
        GridUtils.drawGrid(canvas, mWidth, mainH);
        GridUtils.drawIndexGrid(canvas, indexStartY, mWidth, indexH);
    }

    private void pathSMoveTo(Path sm5_path ,Path sm10_path ,Path sm20_path ){
        if(sma_page_start_index >= IndexParseUtil.START_SMA5 ){
            int position = 0;
            float prices = (float) data.get(sma_page_start_index).getSma5();
            LinePoint curPointList = computerPoints(position ,prices,  xUnit, mainH, 0, DEFUALT_WIDTH / 2, (float) yMax, (float) yMin);
            sm5_path.moveTo(curPointList.x,curPointList.y);
        } else if(sma_page_start_index == 0){
            int position = IndexParseUtil.START_SMA5 ;
            if(data.size() > position){
                float prices = (float) data.get(IndexParseUtil.START_SMA5).getSma5();
                LinePoint curPointList = computerPoints(position ,prices,  xUnit, mainH, 0, DEFUALT_WIDTH / 2, (float) yMax, (float) yMin);
                sm5_path.moveTo(curPointList.x,curPointList.y);
            }
        } else{
            int position = IndexParseUtil.START_SMA5 -sma_page_start_index;
            float prices = (float) data.get(IndexParseUtil.START_SMA5).getSma5();
            LinePoint curPointList = computerPoints(position ,prices,  xUnit, mainH, 0, DEFUALT_WIDTH / 2, (float) yMax, (float) yMin);
            sm5_path.moveTo(curPointList.x,curPointList.y);
        }

        if(sma_page_start_index >= IndexParseUtil.START_SMA10 ){
            int position = 0;
            float prices = (float) data.get(sma_page_start_index).getSma10();
            LinePoint curPointList = computerPoints(position ,prices,  xUnit, mainH, 0, DEFUALT_WIDTH / 2, (float) yMax, (float) yMin);
            sm10_path.moveTo(curPointList.x,curPointList.y);
        } else if(sma_page_start_index == 0){
            int position = IndexParseUtil.START_SMA10 ;
            if(data.size() > position){
                float prices = (float) data.get(position).getSma10();
                LinePoint curPointList = computerPoints(position ,prices,  xUnit, mainH, 0, DEFUALT_WIDTH / 2, (float) yMax, (float) yMin);
                sm10_path.moveTo(curPointList.x,curPointList.y);
            }
        } else{
            int position = IndexParseUtil.START_SMA10 -sma_page_start_index;
            float prices = (float) data.get(IndexParseUtil.START_SMA10).getSma10();
            LinePoint curPointList = computerPoints(position ,prices,  xUnit, mainH, 0, DEFUALT_WIDTH / 2, (float) yMax, (float) yMin);
            sm10_path.moveTo(curPointList.x,curPointList.y);
        }

        if(sma_page_start_index >= IndexParseUtil.START_SMA20){
            int position = 0;
            float prices = (float) data.get(sma_page_start_index).getSma20();
            LinePoint curPointList = computerPoints(position ,prices,  xUnit, mainH, 0, DEFUALT_WIDTH / 2, (float) yMax, (float) yMin);
            sm20_path.moveTo(curPointList.x,curPointList.y);
        } else if(sma_page_start_index == 0){
            int position = IndexParseUtil.START_SMA20 ;
            if(data.size() > position){
                float prices = (float) data.get(position).getSma20();
                LinePoint curPointList = computerPoints(position ,prices, xUnit, mainH, 0, DEFUALT_WIDTH / 2, (float) yMax, (float) yMin);
                sm20_path.moveTo(curPointList.x,curPointList.y);
            }
        } else{
            int position = IndexParseUtil.START_SMA20 -sma_page_start_index;
            float prices = (float) data.get(IndexParseUtil.START_SMA20).getSma20();
            LinePoint curPointList = computerPoints(position ,prices, xUnit, mainH, 0, DEFUALT_WIDTH / 2, (float) yMax, (float) yMin);
            sm20_path.moveTo(curPointList.x,curPointList.y);
        }
    }

    private void pathCMoveTo(Path csm5_path ,Path csm10_path ,Path csm20_path){
        if(sma_page_start_index >= IndexParseUtil.START_SMA5 ){
            int position = 0;
            float prices = (float) data.get(sma_page_start_index).getCountSma5();
            LinePoint curPointList = computerPoints(position ,prices, xUnit, indexH - 2, indexStartY + 2, DEFUALT_WIDTH / 2, volCountMax, 0f);
            csm5_path.moveTo(curPointList.x,curPointList.y);
        } else if(sma_page_start_index == 0){
            int position = IndexParseUtil.START_SMA5;
            if(data.size() > position){
                float prices = (float) data.get(position).getCountSma5();
                LinePoint curPointList = computerPoints(position ,prices, xUnit, indexH - 2, indexStartY + 2, DEFUALT_WIDTH / 2, volCountMax, 0f);
                csm5_path.moveTo(curPointList.x,curPointList.y);
            }
        } else{
            int position = IndexParseUtil.START_SMA5 ;
            float prices = (float) data.get(IndexParseUtil.START_SMA5).getCountSma5();
            LinePoint curPointList = computerPoints(position ,prices, xUnit, indexH - 2, indexStartY + 2, DEFUALT_WIDTH / 2, volCountMax, 0f);
            csm5_path.moveTo(curPointList.x,curPointList.y);
        }

        if(sma_page_start_index >= IndexParseUtil.START_SMA10 ){
            int position = 0;
            float prices = (float) data.get(sma_page_start_index).getCountSma10();
            LinePoint curPointList = computerPoints(position ,prices, xUnit, indexH - 2, indexStartY + 2, DEFUALT_WIDTH / 2, volCountMax, 0f);
            csm10_path.moveTo(curPointList.x,curPointList.y);
        } else if( sma_page_start_index == 0){
            int position = IndexParseUtil.START_SMA10 ;
            if(data.size() > position){
                float prices = (float) data.get(position).getCountSma10();
                LinePoint curPointList = computerPoints(position ,prices, xUnit, indexH - 2, indexStartY + 2, DEFUALT_WIDTH / 2, volCountMax, 0f);
                csm10_path.moveTo(curPointList.x,curPointList.y);
            }
        } else{
            int position = IndexParseUtil.START_SMA10 -sma_page_start_index;
            float prices = (float) data.get(IndexParseUtil.START_SMA10).getCountSma10();
            LinePoint curPointList = computerPoints(position ,prices, xUnit, indexH - 2, indexStartY + 2, DEFUALT_WIDTH / 2, volCountMax, 0f);
            csm10_path.moveTo(curPointList.x,curPointList.y);
        }

        if(sma_page_start_index >= IndexParseUtil.START_SMA20 ){
            int position = 0;
            float prices = (float) data.get(sma_page_start_index).getCountSma20();
            LinePoint curPointList = computerPoints(position ,prices, xUnit, indexH - 2, indexStartY + 2, DEFUALT_WIDTH / 2, volCountMax, 0f);
            csm20_path.moveTo(curPointList.x,curPointList.y);
        } else if( sma_page_start_index == 0){
            int position = IndexParseUtil.START_SMA20 ;
            if(data.size() > position){
                float prices = (float) data.get(IndexParseUtil.START_SMA20).getCountSma20();
                LinePoint curPointList = computerPoints(position,prices, xUnit, indexH - 2, indexStartY + 2, DEFUALT_WIDTH / 2, volCountMax, 0f);
                csm20_path.moveTo(curPointList.x,curPointList.y);
            }
        } else{
            int position = IndexParseUtil.START_SMA20 -sma_page_start_index;
            float prices = (float) data.get(IndexParseUtil.START_SMA20).getCountSma20();
            LinePoint curPointList = computerPoints(position,prices, xUnit, indexH - 2, indexStartY + 2, DEFUALT_WIDTH / 2, volCountMax, 0f);
            csm20_path.moveTo(curPointList.x,curPointList.y);
        }
    }

    @Override
    protected void drawCandles(Canvas canvas) {
        if(data == null)
            return;
        int size = data.size();
        if (size == 0)
            return;
        float x = 0;
        float max_x = 0f;
        float min_x = 0f;
        Path sm5_path = new Path();
        Path sm10_path = new Path();
        Path sm20_path = new Path();

        Path csm5_path = new Path();
        Path csm10_path = new Path();
        Path csm20_path = new Path();

        float gap_value = mWidth / drawCount;
        int position = 0;

        pathSMoveTo(sm5_path ,sm10_path ,sm20_path );
        pathCMoveTo(csm5_path ,csm10_path ,csm20_path );


        double canYmin = Double.MAX_VALUE;
        double canYmax = 0;
        Log.e("page_start_index",sma_page_start_index+"");
        for (int i = sma_page_start_index; i < sma_page_end_inde; i++) {
            x = gap_value * position;
            double high = data.get(i).getHigh();
            double low = data.get(i).getLow();
            if (high >= canYmax) {
                canYmax = high;
                max_x = x;
            }
            if (low <= canYmin) {
                canYmin = low;
                min_x = x;
            }
            DrawUtils.drawCandle(canvas, yMax, yMin,
                    parseNumber(data.get(i).getHigh()),
                    parseNumber(data.get(i).getLow()),
                    parseNumber(data.get(i).getOpen()),
                    parseNumber(data.get(i).getClose()),
                    x,
                    parseNumber(data.get(i).getHigh()),
                    candleXDistance,
                    mWidth);
            DrawUtils.drawVOLRects(canvas, xUnit, indexStartY, indexH, volCountMax, data,i,position);

            if(size != 1){
                int index = i+1;
                int location = position +1;
                smaCountPath(location,index,data, csm5_path, csm10_path,csm20_path);
                smaKLinePath(location,index,data, sm5_path, sm10_path,sm20_path);
            }
            position++;
        }

        line_paint.setColor(ColorUtil.COLOR_SMA5);
        canvas.drawPath(sm5_path,line_paint);
        line_paint.setColor(ColorUtil.COLOR_SMA10);
        canvas.drawPath(sm10_path,line_paint);
        line_paint.setColor(ColorUtil.COLOR_SMA20);
        canvas.drawPath(sm20_path,line_paint);

        line_paint.setColor(ColorUtil.COLOR_SMA5);
        canvas.drawPath(csm5_path,line_paint);
        line_paint.setColor(ColorUtil.COLOR_SMA10);
        canvas.drawPath(csm10_path,line_paint);
        line_paint.setColor(ColorUtil.COLOR_SMA20);
        canvas.drawPath(csm20_path,line_paint);

        DrawUtils.drawVOL(canvas, indexStartY, indexH, volCountMax, ySize);
        drawIndexTexts(canvas);

        DrawUtils.drawCandleMaxAndMin(canvas, instType, canYmax,
                max_x,
                parseNumber(canYmax),
                candleXDistance,
                mWidth,
                true);

        DrawUtils.drawCandleMaxAndMin(canvas, instType, canYmin,
                min_x,
                parseNumber(canYmin),
                candleXDistance,
                mWidth,
                false);
    }


    @Override
    protected void drawText(Canvas canvas) {
        //4，画X轴时间
        if(this.data == null){
            return;
        }
        if (sma_page_end_inde - sma_page_start_index < drawCount / 2) {
            DrawUtils.drawKLineXTime(canvas, data.get(sma_page_start_index).getDate(), null, null, mWidth, mainH, xSize);
        } else if (sma_page_end_inde - sma_page_start_index >= drawCount / 2 && sma_page_end_inde - sma_page_start_index < drawCount) {
            DrawUtils.drawKLineXTime(canvas, data.get(sma_page_start_index).getDate(), data.get(sma_page_start_index+drawCount / 2 - 1).getDate(), null, mWidth, mainH, xSize);
        } else {
            DrawUtils.drawKLineXTime(canvas, data.get(sma_page_start_index).getDate(), data.get(sma_page_start_index+drawCount / 2 - 1).getDate(), data.get(sma_page_end_inde - sma_page_start_index - 1).getDate(), mWidth, mainH, xSize);
        }
        //5，画Y轴价格
        DrawUtils.drawKLineYPrice(instType, canvas, yMax, yMin, mainH, ySize);
    }


    private void drawIndexTexts(Canvas canvas) {
        if (!isCrossLineVisiable ) {
            currentData = data.get(data.size() - 1);
        }
        CrossBean bean = new CrossBean();
        bean.indexText = new String[4];
        bean.indexText[0] = "VOL(5,10,20)";
        bean.indexText[1] = "MA5:" + StringConvertNumber.volumeToString((float) currentData.getCountSma5());
        bean.indexText[2] = "MA10:" + StringConvertNumber.volumeToString((float) currentData.getCountSma10());
        bean.indexText[3] = "MA20:" + StringConvertNumber.volumeToString((float) currentData.getCountSma20());
        bean.indexColor = new int[]{
                currentData.isRise() ? ColorUtil.COLOR_RED : ColorUtil.COLOR_GREEN,
                ColorUtil.COLOR_SMA5,
                ColorUtil.COLOR_SMA10,
                ColorUtil.COLOR_SMA20};
        if (bean.indexText == null) return;
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setTextSize(maValueSize);
        float x = 0;
        float y = getHeight() * (ChartConstant.MAIN_SCALE + ChartConstant.TIME_SCALE) - 4;
        for (int i = 0; i < bean.indexText.length; i++) {
            p.setColor(bean.indexColor[i]);

            canvas.drawText(bean.indexText[i], x, y, p);
            x += LineUtil.getTextWidth(p, bean.indexText[i]) + 30;
        }

    }


    /**
     * 把传入的参数计算成坐标，直接展示到界面上
     *
     * @param input
     * @return 返回里面的StickData的最高价最低价，都是可以直接显示在坐标上的
     */
    private float parseNumber(double input) {
        return mainH - (float) ((input - yMin) / yUnit)  - 20;
    }



    public void lineSPath(Path path,int type,int position,int id,List<StickData> list, float xUnit, float height, float y, float xOffset, float maxYValue, float minYValue) {
        int size = list.size();
        if(size >= 2){
            float p_pre_prices = 0f;
            float pre_prices = 0f;
            float cur_prices = 0f;
            float next_prices = 0f;
            if(type == 4){
                p_pre_prices = (float) list.get((id - type) == 1 ? id - 1 : id - 2).getSma5();
                pre_prices = (float) list.get(id - 1).getSma5();
                cur_prices = (float) list.get(id >= size ? size -1 : id ).getSma5();
                next_prices = size > id + 1 ? (float) list.get(id + 1).getSma5() : cur_prices;
            }
            if(type == 9){
                p_pre_prices = (float) list.get((id - type) == 1 ? id - 1 : id - 2).getSma10();
                pre_prices = (float) list.get(id - 1).getSma10();
                cur_prices = (float) list.get(id >= size ? size -1 : id ).getSma10();
                next_prices = size > id + 1 ? (float) list.get(id + 1).getSma10() : cur_prices;
            }
            if(type == 19){
                p_pre_prices = (float) list.get((id - type) == 1 ? id - 1 : id - 2).getSma20();
                pre_prices = (float) list.get(id - 1).getSma20();
                cur_prices = (float) list.get(id >= size ? size -1 : id  ).getSma20();
                next_prices = size > id + 1 ? (float) list.get(id + 1).getSma20() : cur_prices;
            }
            LinePoint p_prePointList = computerPoints(position - 2 < 0 ? 0 : position - 2,p_pre_prices, xUnit, height,y, xOffset, maxYValue, minYValue);
            LinePoint prePointList = computerPoints(position -1,pre_prices, xUnit, height, y, xOffset, maxYValue, minYValue);
            LinePoint curPointList = computerPoints(position,cur_prices, xUnit, height, y, xOffset, maxYValue, minYValue);
            LinePoint nexPointList = computerPoints(size > id + 1 ? position + 1 : position,next_prices, xUnit, height, y, xOffset, maxYValue, minYValue);
            drawCubicBezier(path,p_prePointList,prePointList,curPointList,nexPointList);
        }
    }


    public void lineCPath(Path path,int type,int position,int id,List<StickData> list, float xUnit, float height, float y, float xOffset, float maxYValue, float minYValue) {
        int size = list.size();
        if(size >= 2){
            float p_pre_prices = 0f;
            float pre_prices = 0f;
            float cur_prices = 0f;
            float next_prices = 0f;
            if(type == 4){
                p_pre_prices = (float) list.get((id - type) == 1 ? id - 1 : id - 2).getCountSma5();
                pre_prices = (float) list.get(id - 1).getCountSma5();
                cur_prices = (float) list.get(id >= size ? size -1 : id ).getCountSma5();
                next_prices = size > id + 1 ? (float) list.get(id + 1).getCountSma5() : cur_prices;
            }
            if(type == 9){
                p_pre_prices = (float) list.get((id - type) == 1 ? id - 1 : id - 2).getCountSma10();
                pre_prices = (float) list.get(id - 1).getCountSma10();
                cur_prices = (float) list.get(id >= size ? size -1 : id  ).getCountSma10();
                next_prices = size > id + 1 ? (float) list.get(id + 1).getCountSma10() : cur_prices;
            }
            if(type == 19){
                p_pre_prices = (float) list.get((id - type) == 1 ? id - 1 : id - 2).getCountSma20();
                pre_prices = (float) list.get(id - 1).getCountSma20();
                cur_prices = (float) list.get(id >= size ? size -1 : id ).getCountSma20();
                next_prices = size > id + 1 ? (float) list.get(id + 1).getCountSma20() : cur_prices;
            }
            LinePoint p_prePointList = computerPoints(position - 2 < 0 ? 0 : position - 2,p_pre_prices, xUnit, height,y, xOffset, maxYValue, minYValue);
            LinePoint prePointList = computerPoints(position - 1,pre_prices, xUnit, height, y, xOffset, maxYValue, minYValue);
            LinePoint curPointList = computerPoints(position ,cur_prices, xUnit, height, y, xOffset, maxYValue, minYValue);
            LinePoint nexPointList = computerPoints(size > id + 1 ? position + 1 : position,next_prices, xUnit, height, y, xOffset, maxYValue, minYValue);
            drawCubicBezier(path,p_prePointList,prePointList,curPointList,nexPointList);
        }
    }

    private void smaCountPath(int position,int id, List<StickData> list, Path sm5_path, Path sm10_path, Path sm20_path){
        if(id > 4){
            lineCPath(sm5_path,4,position,id,list, DEFUALT_WIDTH, indexH - 2, indexStartY + 2, DEFUALT_WIDTH / 2,volCountMax, 0f);
        }

        if(id > 9){
            lineCPath(sm10_path,9,position,id,list, DEFUALT_WIDTH, indexH - 2, indexStartY + 2, DEFUALT_WIDTH / 2,volCountMax, 0f);
        }

        if(id > 19){
            lineCPath(sm20_path,19,position,id,list, DEFUALT_WIDTH, indexH - 2, indexStartY + 2, DEFUALT_WIDTH / 2,volCountMax, 0f);
        }
    }

    private void smaKLinePath(int position,int id, List<StickData> list, Path sm5_path, Path sm10_path, Path sm20_path){
        if(id > 4){
            lineSPath(sm5_path,4,position,id,list, DEFUALT_WIDTH, mainH, 0, DEFUALT_WIDTH / 2, (float) yMax, (float) yMin);
        }

        if(id > 9){
            lineSPath(sm10_path,9,position,id,list, DEFUALT_WIDTH, mainH, 0, DEFUALT_WIDTH / 2, (float) yMax, (float) yMin);
        }

        if(id > 19){
            lineSPath(sm20_path,19,position,id,list, DEFUALT_WIDTH, mainH, 0, DEFUALT_WIDTH / 2, (float) yMax, (float) yMin);
        }
    }


    public static LinePoint computerPoints(int i, float prices, float xUnit, float height, float y, float xOffset, float maxYValue, float minYValue) {
        float yUnit = (maxYValue - minYValue) / height;
        LinePoint pointt;
        pointt = new LinePoint();
        pointt.x = xOffset + i * xUnit;
        pointt.y = y + height - (prices - minYValue) / yUnit;
        if(pointt.y > y + height){
            pointt.y = y + height;
        }
        if(pointt.y < y){
            pointt.y = y;
        }
        return pointt;
    }

    protected static void drawCubicBezier(Path path, LinePoint prevPrev, LinePoint prev, LinePoint cur, LinePoint next) {
        float phaseX = 1.0f;
        float phaseY = 1.0f;
        float prevDx = 0f;
        float prevDy = 0f;
        float curDx = 0f;
        float curDy = 0f;
        if(cur != null && prevPrev != null && prev != null && next != null){
            prevDx = (cur.x - prevPrev.x) * lineSmoothness;
            prevDy = (cur.y - prevPrev.y) * lineSmoothness;
            curDx = (next.x - prev.x) * lineSmoothness;
            curDy = (next.y - prev.y) * lineSmoothness;
            path.cubicTo(prev.x + prevDx, (prev.y + prevDy) * phaseY,
                    cur.x - curDx,
                    (cur.y - curDy) * phaseY, cur.x, cur.y * phaseY);
        }

    }

    public void setDataAndInvalidate(List<StickData> data, final Market.InstType instType) {
        this.data = data;
        this.instType = instType;
        invalidate();
    }

    /**
     * 获取页面一页可展示的数据
     */
    private synchronized void getShowList(int offset) {
        if (offset != 0 && data.size() - drawCount - offset < 0) {
            offset = data.size() - drawCount;
        }
        int size = data.size();
        sma_page_start_index = size - drawCount - offset;
        sma_page_end_inde = size - offset;
        if (size == 0) {
            return;
        }
        if (sma_page_end_inde >= size || sma_page_start_index >= size || sma_page_start_index >= size - drawCount) {
            sma_page_end_inde = size;
            sma_page_start_index = size - drawCount;
        }
        if (sma_page_start_index <= 0) {
            sma_page_start_index = 0;
        }
        if (size <= drawCount) {
            sma_page_start_index = 0;
            sma_page_end_inde = size;
        }

        if (!isCrossLineVisiable ) {
            currentData = data.get(data.size() - 1);
        }
    }

    /**
     * 设置K线类型
     */
    public void setType(int type) {
        lineType = type;
    }

    @Override
    public void onCrossMove(float x, float y) {
        super.onCrossMove(x, y);
        if (crossView == null || this.data == null) return;
        int position = (int) Math.rint(new Double(x) / new Double(DEFUALT_WIDTH));
        if (position > sma_page_end_inde - sma_page_start_index) {
            position = sma_page_end_inde - sma_page_start_index - 1;
        }
        if (position >= 1 && position < sma_page_end_inde - sma_page_start_index) {
            StickData data = this.data.get(sma_page_start_index+position);
            isCrossLineVisiable = true;
            currentData = data;
            StickData preData = this.data.get(sma_page_start_index+position - 1);
            float xIn = (mWidth / drawCount * position) + (mWidth / candleXDistance / 2);
            CrossBean bean = new CrossBean(xIn, getY(data.getClose()));
            bean.price = data.getClose() + "";
            //注意，这个值用来区分，是否画出均线的小圆圈
            bean.y2 = -1;
            bean.volume = data.getCount();
            bean.timeYMD = data.getDate();
            bean.setIndexTexts(data, indexType);
            crossView.drawLine(instType, bean, 2);
            postInvalidate();
            if (crossView.getVisibility() == GONE)
                crossView.setVisibility(VISIBLE);
            //TODO 这里显示该点数据到屏幕
            msgText.setVisibility(VISIBLE);
            msgText.setText(Html.fromHtml(ColorUtil.getCurPriceInfo(data, instType)));
            if (onCrossListener != null) {
                onCrossListener.onShow(data, preData);
            }
        }
    }

    @Override
    public void onDismiss() {
        isCrossLineVisiable = false;
        postInvalidate();
        msgText.setText(Html.fromHtml(ColorUtil.getCurPriceInfo(data.get(data.size() - 1), instType)));
        if (onCrossListener != null) {
            onCrossListener.onDismiss();
        }
    }

    //获取价格对应的Y轴
    private float getY(double price) {
        return mainH - (new Float(price) - (float) yMin) / yUnit;
    }


    public interface OnCrossListener {
        //十字线消失
        void onDismiss();

        //十字线显示
        void onShow(StickData data, StickData preData);
    }
}
